/**
 *
 */
package com.naiterui.ehp.bs.doctor.dao.impl;

import com.naiterui.ehp.bp.constants.CommonConstant;
import com.naiterui.ehp.bp.domain.Hospital;
import com.naiterui.ehp.bs.doctor.dao.IHospitalDao;
import com.naiterui.ehp.bs.doctor.repository.IHospitalRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 医院持久层实现类
 */
@Component
public class AdHospitalDaoImpl implements IHospitalDao {

    @Autowired
    private IHospitalRepository hospitalRepository;

    @Override
    public Hospital get(Integer id) {
        return hospitalRepository.get(id);
    }

    @Override
    public List<Hospital> getHospitalListByCity(Integer cityId, String name, Integer max) {
        if (null != max) {
            if (!StringUtils.isEmpty(name)) {
                return hospitalRepository.findByNameLikeAndHideLimit("%" + name + "%", CommonConstant.HOSPITAL_DISPLAY_SHOW, max);
            } else {
                return hospitalRepository.findByCityIdAndHideLimit(cityId,CommonConstant.HOSPITAL_DISPLAY_SHOW, max);
            }
        } else {
            if (!StringUtils.isEmpty(name)) {
                return hospitalRepository.findByNameLikeAndHideOrderByCreatedAtDesc("%" + name + "%", CommonConstant.HOSPITAL_DISPLAY_SHOW);
            } else {
                return hospitalRepository.findByCityIdAndHideOrderByChangedAtDesc(cityId,CommonConstant.HOSPITAL_DISPLAY_SHOW);
            }
        }
    }

    /**
     * 根据城市列表查询医院
     * @Author guoyongxiang
     * @Date 2015年12月2日
     * @since 1.5.0
     * @param cityIds
     * @return
     * @see IHospitalDao#getHospitalListByCitys(java.util.List)
     */
    @Override
    public List<Hospital> getHospitalListByCitys(List<Integer> cityIds) {
        List<Hospital> hospitals = new ArrayList<>();
        if (cityIds.size() > 0) {
            hospitals = hospitalRepository.findByCityIdInAndHideOrderByChangedAtDesc(cityIds, CommonConstant.HOSPITAL_DISPLAY_SHOW);
        }
        return hospitals;
    }

    @Override
    public List<Hospital> getHospitalListByCitysLimit(List<Integer> cityIds, Integer max) {
        List<Hospital> hospitals = new ArrayList<>();
        if (cityIds.size() > 0) {
            hospitals = hospitalRepository.findByCityIdAndHideLimit(cityIds, CommonConstant.HOSPITAL_DISPLAY_SHOW, max);
        }
        return hospitals;
    }

    /**
     * 根据医院id查询医院信息
     * HospitalDaoImpl.getHospitalVo()
     * <p>@Param : hospitalId</p>
     * <p>@Author : linhf</p>
     * <p>@Date : 2016年5月3日 下午5:50:40</p>
     * @return :
     */
    @Override
    public Hospital getHospitalById(Integer hospitalId) {
        return hospitalRepository.get(hospitalId);
    }
}
